package com.glaive;

import com.glaive.utils.ChineseNum2int;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TxtIndexHandleCmd20230103 {
    static String filePath = System.getProperty("user.dir") + "\\二月河-康熙大帝20230103.txt";

    public static void main(String[] args) throws Exception {
        readLine();
    }


    public static String defaultIndexString = "回";

    /**
     *
     */
    public static void readLine() throws Exception {
        //正则说明：
        // 1. 三位数加空格开头
        Pattern pattern = Pattern.compile(
                "　　(第(.*?)" + defaultIndexString + ")　(.*)"
        );

        Files
                .lines(Paths.get(filePath))
                .map(s -> {
                    Matcher m = pattern.matcher(s);
                    if (m.find()) {
                        //调试时使用
//                        System.out.println(m.group(1) + "," + m.group(2) + "," + m.group(3));
                        int xx = ChineseNum2int.chineseNumToArabicNum(m.group(2));
                        String volume = "第" + xx + defaultIndexString;
                        String indexSection = m.group(3);
                        String index = volume +" "+ indexSection;
//                        System.out.println(index);
                        return index;
//                        return "";
                    } else
                        return s;
//                        return "";
                })
//                .distinct()
                .forEach(s -> {
                    System.out.println(s);
                });
    }

    static String volumeSection = "";

    /**
     * 索引 去重
     *
     * @param volume
     * @param section
     * @return
     */
    private static boolean indexIsRepeat(String volume, String section) {
        if (volumeSection.equals(volume + section)) {
            return true;
        } else {
            volumeSection = volume + section;
            return false;
        }

    }

    static String volumeTmp = "";

    /**
     * 卷的索引处理：只有第一卷的首次出现 都会显示卷的标题，其他省略
     */
    private static String volumeHandle(String volume, String volumeValue) {
        if (!volumeTmp.equals(volume + volumeValue)) {
            volumeTmp = volume + volumeValue;
            return volumeTmp;
        } else {
            return volume;
        }
    }
}